# 1.导包
import requests

# 2.确定url
url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2023-08-09&leftTicketDTO.from_station=TYV&leftTicketDTO.to_station=EAY&purpose_codes=ADULT'

# 3.伪装
head = {
    'Cookie': '_uab_collina=169121996859784853561466; JSESSIONID=DEB52B05D6A3DD9E7061B5CDC58EF361; BIGipServerotn=1473839370.50210.0000; BIGipServerpassport=1005060362.50215.0000; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; fo=2h8cn6b56v7e3ss8gxyGQgBzkiSuDTzijXoKKGVzscAKLJ084GCJbSyld_waTh9Xnx7A-s9E9Lz_zVdHtuIt0AN3tVVpYQgLQAHc3CXzRSRBZj1Vl_NTaUgP_1mT6mVa-txk79MyyFcg5BzVgVtqf24OYQT4VagbZ2NA7KbevbgRdm9fsvR8_l9ai1s; route=9036359bb8a8a461c164a04f8f50b252; _jc_save_fromStation=%u592A%u539F%2CTYV; _jc_save_toStation=%u897F%u5B89%2CEAY; _jc_save_toDate=2023-08-05; _jc_save_wfdc_flag=dc; _jc_save_fromDate=2023-08-09'
}

# 4.发送请求
res = requests.get(url, headers=head)

# 5.将请求得到的数据用字典呈现
json_data = res.json()
# print(json_data)

# 6.将需要的东西截取出来
data = json_data['data']['result']
# 定义一个输出主题格式
format_title = "{:^6}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t{:^2}\t"
print(format_title.format('车次', '上车时间', '下车时间', '历时', '一等座', '二等座'))
for i in data:
    result = i.split("|")
    #定义一个输出内容格式
    format_content = "{:^6}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t{:^2}\t"
    print(format_content.format(result[3], result[8], result[9], result[10], result[31], result[30]))
    # for index,value in enumerate(result):
    #     print(index,value)
    # break
